package com.huawei.hwid.core.b.a;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.text.TextUtils;
import com.huawei.cloudservice.CloudRequestHandler;
import com.huawei.hms.support.api.entity.sns.SNSCode.Status;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.d.m;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;
import org.xmlpull.v1.XmlPullParserException;

public abstract class a {
    protected int a = 200;
    protected int b = -1;
    protected int c = -1;
    protected String d;
    protected String e;
    protected int f = 3;
    protected int g = 1;
    private ArrayList<Integer> h = new ArrayList();
    private boolean i = false;
    private boolean j = false;
    private boolean k = true;
    private int l = 0;
    private String m;
    private String n;
    private String o;
    private ArrayList<String> p = new ArrayList();
    private c q = c.XMLType;

    static class a extends HandlerThread {
        private b a = null;
        private com.huawei.hwid.core.helper.handler.a b = null;

        a(String str, com.huawei.hwid.core.helper.handler.a aVar) {
            super(str);
            this.b = aVar;
        }

        protected void onLooperPrepared() {
            this.a = new b(this.b);
            super.onLooperPrepared();
        }

        public b a() {
            int i = 1000;
            while (this.a == null) {
                int i2 = i - 1;
                if (i <= 0) {
                    break;
                }
                try {
                    sleep(4);
                    i = i2;
                } catch (InterruptedException e) {
                    e.d("HttpRequest", "InterruptedException", true);
                    i = i2;
                }
            }
            return this.a;
        }
    }

    public static class b extends Handler {
        private com.huawei.hwid.core.helper.handler.a a;

        b(com.huawei.hwid.core.helper.handler.a aVar) {
            this.a = aVar;
        }

        public void a() {
            getLooper().quit();
        }

        public void handleMessage(Message message) {
            if (message.what == 0) {
                this.a.e((Bundle) message.obj);
                a();
            }
            super.handleMessage(message);
        }
    }

    public enum c {
        XMLType,
        URLType,
        JSONType,
        Stream,
        FileType
    }

    static class d {
        private static String a = com.huawei.hwid.vermanager.c.a().a();
    }

    protected abstract void a(String str) throws XmlPullParserException, IOException;

    public abstract String b();

    protected abstract String c(Context context);

    protected abstract String j() throws IllegalArgumentException, IllegalStateException, IOException;

    public static String a() {
        return d.a;
    }

    public void a(Context context, a aVar, String str, CloudRequestHandler cloudRequestHandler) {
    }

    public Bundle c() {
        return e();
    }

    public c d() {
        return this.q;
    }

    public void a(c cVar) {
        this.q = cVar;
    }

    public Bundle e() {
        Bundle bundle = new Bundle();
        bundle.putInt("responseCode", this.a);
        bundle.putInt("resultCode", this.b);
        bundle.putInt("errorCode", this.c);
        bundle.putString("errorDesc", this.d);
        bundle.putString("TGC", this.e);
        bundle.putIntegerArrayList("UIHandlerErrCodeList", m());
        bundle.putBoolean("isUIHandlerAllErrCode", s());
        bundle.putBoolean("isIngoreTokenErr", t());
        return bundle;
    }

    public int f() {
        return this.a;
    }

    public void a(int i) {
        this.a = i;
    }

    public int g() {
        return this.b;
    }

    public int h() {
        return this.c;
    }

    public String i() {
        return this.d;
    }

    protected String k() {
        return null;
    }

    protected void b(String str) {
    }

    private boolean s() {
        return this.i;
    }

    public void a(boolean z) {
        this.i = z;
    }

    private boolean t() {
        return this.j;
    }

    public int l() {
        return this.g;
    }

    public void b(int i) {
        this.h.add(Integer.valueOf(i));
    }

    public ArrayList<Integer> m() {
        return this.h;
    }

    public String n() {
        String b = b();
        if (TextUtils.isEmpty(b)) {
            return "";
        }
        return b.substring(b.lastIndexOf("/") + 1);
    }

    public void a(Context context, int i) {
        this.l = i;
        String str = "";
        String b = b();
        String a = com.huawei.hwid.d.b.a().a(context, i);
        e.b("HttpRequest", "setGlobalSiteId, AsUrl::=" + a, false);
        this.m = a + b;
        if (this.l >= 1 && this.l <= 999) {
            str = String.valueOf(i);
        }
        this.m = m.a(this.m, new String[]{"\\{0\\}", str});
        e.b("HttpRequest", "Set mGlobalHostUrl::=" + this.m, false);
    }

    public int o() {
        return this.l;
    }

    public String a(Context context) {
        if (TextUtils.isEmpty(this.m)) {
            return b() + "?Version=" + "27120" + "&cVersion=" + com.huawei.hwid.core.d.b.n(context);
        }
        return this.m + "?Version=" + "27120" + "&cVersion=" + com.huawei.hwid.core.d.b.n(context);
    }

    public void c(String str) {
        this.m = str;
    }

    public boolean p() {
        return this.k;
    }

    public int q() {
        return this.f;
    }

    public void c(int i) {
        this.f = i;
    }

    protected b a(Context context, a aVar, com.huawei.hwid.core.helper.handler.a aVar2) {
        a aVar3 = new a("BackgroundHandlerThread", aVar2);
        aVar3.start();
        return aVar3.a();
    }

    private String u() {
        return this.n;
    }

    public void d(String str) {
        this.n = str;
    }

    private String v() {
        return this.o;
    }

    public void e(String str) {
        this.o = str;
    }

    public void a(int i, int i2, String str) {
        if (i2 == 200 && this.b != 0 && this.c > 0) {
            i2 = this.c;
            str = i();
        }
        ArrayList arrayList = this.p;
        Locale locale = Locale.ROOT;
        String str2 = "{times:%d,code:%d,msg:%s}";
        Object[] objArr = new Object[3];
        objArr[0] = Integer.valueOf(i);
        objArr[1] = Integer.valueOf(i2);
        if (TextUtils.isEmpty(str)) {
            str = "result ok";
        }
        objArr[2] = str;
        arrayList.add(String.format(locale, str2, objArr));
    }

    private String w() {
        if (this.p.isEmpty()) {
            return "";
        }
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("{");
        int size = this.p.size();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                if (i != 0) {
                    stringBuilder.append(",");
                }
                stringBuilder.append((String) this.p.get(i));
            }
        }
        stringBuilder.append("}");
        return stringBuilder.toString();
    }

    public void r() {
        this.p.clear();
    }

    public Bundle b(Context context) {
        Object w = w();
        if (TextUtils.isEmpty(w)) {
            return null;
        }
        Bundle bundle = new Bundle();
        bundle.putString(HwAccountConstants.EXTRA_OPLOG_OPID, "0");
        bundle.putString(HwAccountConstants.EXTRA_OPLOG_REQTIME, u());
        bundle.putString(HwAccountConstants.EXTRA_OPLOG_RSPTIME, v());
        bundle.putString(HwAccountConstants.EXTRA_OPLOG_OPDETAIL, w);
        bundle.putString("url", a(context));
        bundle.putInt(HwAccountConstants.EXTRA_OPLOG_SITEID, o());
        int f = f();
        if (1008 == f || 1005 == f || Status.GET_USER_UNREAD_MSG_FAIL == f || x()) {
            bundle.putBoolean(HwAccountConstants.EXTRA_OPLOG_IS_REQUEST_EXCEPTION, true);
            bundle.putString(HwAccountConstants.EXTRA_OPLOG_ERROR, String.valueOf(f));
            return bundle;
        }
        int g = g();
        if (-1 != g) {
            bundle.putString(HwAccountConstants.EXTRA_OPLOG_ERROR, String.valueOf(g));
        } else if (-1 != h()) {
            bundle.putString(HwAccountConstants.EXTRA_OPLOG_ERROR, String.valueOf(h()));
        } else {
            bundle.putString(HwAccountConstants.EXTRA_OPLOG_ERROR, String.valueOf(f));
        }
        bundle.putBoolean(HwAccountConstants.EXTRA_OPLOG_IS_REQUEST_EXCEPTION, false);
        return bundle;
    }

    private boolean x() {
        return (200 == f() || 307 == f()) ? false : true;
    }
}
